#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
#include"cxcore.h"

IplImage* GetThresholdedImage(IplImage* imgHSV){
    IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
    cvInRangeS(imgHSV, cvScalar(170,160,60,0), cvScalar(180,255,256,0), imgThresh);
    return imgThresh;
}


void create_histogram_image(IplImage*, IplImage*);




int main(){
      CvCapture* capture =0;

capture = cvCaptureFromCAM(1);
      if(!capture){
            printf("Capture failure\n");
            return -1;
      }

      IplImage* frame=0;
      frame = cvQueryFrame(capture);
       if(!frame) return -1;
       //create a blank image and assigned to 'imgTracking' which has the same size of original video
         cvNamedWindow("Video",0);
      cvNamedWindow("first",0);

/*IplImage* frame1=0;
frame1 = cvQueryFrame(capture);
if(!frame1) return -1;


      cvNamedWindow("second",0);*/



                     //iterate through each frames of the video
      while(1){

            frame = cvQueryFrame(capture);
            if(!frame) break;

            frame=cvCloneImage(frame);
            cvSmooth(frame, frame, CV_GAUSSIAN,3,3,0,0); //smooth the original image using Gaussian kernel

            IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
            //cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV

            IplImage* imgGrayScale=cvCreateImage(cvGetSize(imgHSV),8,1);
cvCvtColor(frame, imgGrayScale, CV_BGR2GRAY);
             IplImage* imgThresh = GetThresholdedImage(imgHSV);

          //  cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3,0,0); //smooth the binary image using Gaussian kernel
//cvCvtColor(frame, imgGrayScale, CV_BGR2GRAY);
cvNamedWindow("imggray",0);
cvShowImage("imggray",imgGrayScale);



IplImage *hist1 = cvCreateImage(cvSize(640,480), 8, 1);
    cvSet( hist1, cvScalarAll(255), 0 );
    IplImage *hist2 = cvCreateImage(cvSize(640,480), 8, 1);
    cvSet( hist2, cvScalarAll(255), 0 );

       create_histogram_image(imgGrayScale, hist1);
       cvEqualizeHist( imgHSV, imgGrayScale );// Perform histogram equalization
       create_histogram_image(imgGrayScale, hist2);

    cvNamedWindow( "Original", 0) ;// Show original
    cvShowImage( "Original", imgGrayScale );
    cvNamedWindow( "Histogram before equalization", 1) ;//Show Histograms
    cvShowImage( "Histogram before equalization", hist1 );
    cvNamedWindow( "Histogram after equalization", 1) ;
    cvShowImage( "Histogram after equalization", hist2 );
    cvNamedWindow("Result", 0) ; // Show histogram equalized
    cvShowImage("Result", imgGrayScale );




            //cvCvtColor(frame, imgGrayScale, CV_BGR2GRAY);
         /*   cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
           cvNamedWindow("first",CV_WINDOW_AUTOSIZE);


            cvShowImage("first", imgGrayScale);
            cvShowImage("first",imgThresh);
           // cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);

             CvMat* disparity_left  = cvCreateMat(imgThresh->height, imgThresh->width, CV_16S);



          //  cvShowImage("Video", frame);



          /*  IplImage* imgHSV1 = cvCreateImage(cvGetSize(frame1), IPL_DEPTH_8U, 3);
            cvCvtColor(frame1, imgHSV1, CV_BGR2HSV); //Change the color format from BGR to HSV
            IplImage* imgGrayScale1=cvCreateImage(cvGetSize(imgHSV1),8,1);

            IplImage* imgThresh1 = GetThresholdedImage(imgHSV1);

            cvSmooth(imgThresh1, imgThresh1, CV_GAUSSIAN,3,3,0,0);
            cvNamedWindow("second1",CV_WINDOW_AUTOSIZE);
cvShowImage("second1", imgThresh1);

            cvCvtColor(frame1, imgGrayScale1, CV_BGR2GRAY);

             cvNamedWindow("second",CV_WINDOW_AUTOSIZE);
cvShowImage("second", imgGrayScale);
cvShowImage("Video", frame);

            //cvThreshold(imgGrayScale1, imgGrayScale1, 128, 255, CV_THRESH_BINARY);

            CvMat* disparity_right = cvCreateMat(imgThresh1->height, imgThresh1->width, CV_16S);


            CvStereoBMState* state = cvCreateStereoBMState(16,128);



            cvFindStereoCorrespondenceBM(imgThresh,imgThresh1,disparity_left,state);
CvMat* disp_left_visual = cvCreateMat(imgThresh->height, imgThresh->width, CV_8U);
cvConvertScale( disparity_left, disp_left_visual, -20 ,0);
cvSave( "disparity.pgm", disp_left_visual,0,0,cvAttrList(0,0) );
cvThreshold(disp_left_visual,imgThresh,120,120,CV_THRESH_BINARY);
cvNamedWindow("Disparity map", CV_WINDOW_AUTOSIZE);
cvShowImage("Disparity map", disp_left_visual);

           //cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);*/





     cvWaitKey(0);


     cvReleaseCapture(&capture);

    cvReleaseImage(&frame);
     return 0;

      }

}


void create_histogram_image(IplImage* gray_img, IplImage* hist_img) {
  CvHistogram *hist;
  int hist_size = 256;
  float range[]={0,256};
  float* ranges[] = { range };
  float max_value = 0.0;
  float w_scale = 0.0000000;

int i;
hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);// create array to hold histogram values

cvCalcHist( &gray_img, hist, 0, NULL );// calculate histogram values

cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); // Get the minimum and maximum values of the histogram

cvScale( hist->bins, hist->bins, ((float)hist_img->height)/max_value, 0 );// set height by using maximim value

w_scale = ((float)hist_img->width)/hist_size;// calculate width


// plot the histogram
 for(  i = 0; i < hist_size; i++ ) {
 cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
 cvPoint((int)(i+1)*w_scale, hist_img->height -

 cvRound(cvGetReal1D(hist->bins,i))),
 cvScalar(0,0,0,0), -1, 8, 0 );
    }
}










